#include "stdafx.h"
#include "../inc/IApp.h"

ILauncher::ILauncher(Engine *pengine):IApp(pengine)
{
	
}

ILauncher::~ILauncher()
{
	AppPtrList::iterator it;
	for (it=m_AppPtrList.begin();it!=m_AppPtrList.end();it++)
	{
		pIApp pcur_app=*it;
		delete pcur_app;
		pcur_app=NULL;
	}
	m_AppPtrList.clear();

}

bool ILauncher::HasApp(IApp *papp)
{
	bool has=false;
	AppPtrList::iterator it;
	for (it=m_AppPtrList.begin();it!=m_AppPtrList.end();it++)
	{
		pIApp pcur_app=*it;
		if (pcur_app==papp)
		{
			has=true;
			return has;
		}
	}
	return has;
}

bool ILauncher::AddApp(IApp *papp)
{
	bool ok=true;
	if (!papp)
	{
		ok=false;
		return ok;
	}
	if (HasApp(papp))
	{
		return ok;
	}
	m_AppPtrList.push_back(papp);

	m_AppIt=m_AppPtrList.begin();

	return ok;
}

bool ILauncher::RemoveApp(pIApp papp)
{
	bool ok=true;
	m_AppPtrList.remove(papp);
	return ok;
}

pIApp ILauncher::GetApp(int id)
{
	pIApp papp=NULL;
	AppPtrList::iterator it;
	for (it=m_AppPtrList.begin();it!=m_AppPtrList.end();it++)
	{
		pIApp pcur_app=*it;
		if (pcur_app->GetAppID()==id)
		{
			papp=pcur_app;
			return papp;
		}
	}
	return papp;
}

bool ILauncher::NextApp()
{
	bool has=false;
	if (m_AppIt!=m_AppPtrList.end())
	{
		has=true;
	}
	if (m_AppIt==m_AppPtrList.end())
	{
		m_AppIt=m_AppPtrList.begin();
	}

	return has;

}

IApp *ILauncher::GetApp()
{
	IApp *papp=NULL;
	papp=*m_AppIt;

	m_AppIt++;

	return papp;
}